9. Strings

Note

The below information is extensively based in information taken from the PowerShell® Notes for Professionals book. I plan to extend this information based on my day to day usage of the language.

9.1: Multiline string

There are multiple ways to create a multiline string in PowerShell:

1
2
You can use the special characters for carriage return and/or newline manually or use the NewLine-
environment variable to insert the systems "newline" value)
1
2
"Hello `r`n World"
"Hello{0}World" -f [environment]::NewLine
1
Create a linebreak while defining a string (before closing quote)
1
2
"Hello
World"
1
Using a here-string. This is the most common technique.
1
2
3
4
@"
Hello
World
"@

9.2: Here-string

Here-strings are very useful when creating multiline strings. One of the biggest benefits compared to other multiline strings are that you can use quotes without having to escape them using a backtick.

Here-string

Here-strings begin with @" and a linebreak and end with "@ on its own line ( "@ must be first characters on the line, not even whitespace/tab ).

1
2
3
4
5
@"
Simple
Multiline string
with "quotes"
"@

Literal here-string

You could also create a literal here-string by using single quotes, when you don't want any expressions to be expanded just like a normal literal string.

1
2
3
4
5
@'
The following line won't be expanded
$( Get-Date )
because this is a literal here-string
'@

9.3: Concatenating strings

Using variables in a string

You can concatenate strings using variables inside a double-quoted string. This does not work with properties.

1
2
3
$string1 = "Power"
$string2 = "Shell"
"Greetings from $string1$string2"

Using the + operator

You can also join strings using the + operator.

1
2
3
$string1 = "Greetings from"
$string2 = "PowerShell"
$string1 + " " + $string2

This also works with properties of objects.

1
"The title of this console is '" + $host.Name + "'"

Using subexpressions

The output/result of a subexpressions $() can be used in a string. This is useful when accessing properties of an object or performing a complex expression. Subexpressions can contain multiple statements separated by semicolon ;

1
"Tomorrow is $((Get-Date).AddDays(1).DayOfWeek)"

9.4: Special characters

When used inside a double-quoted string, the escape character (backtick `) represents a special character.

1
2
3
4
5
6
7
8
`0  #Null
`a  #Alert/Beep
`b  #Backspace
`f  #Form feed (used for printer output)
`n  #New line
`r  #Carriage return
`t  #Horizontal tab
`v  #Vertical tab (used for printer output)

Example:

1
2
3
"This `t uses `t tab `r`n This is on a second line"
This uses tab
This is on a second line

You can also escape special characters with special meanings:

1
2
3
4
5
`#  #Comment-operator
`$  #Variable operator
``  #Escape character
`'  #Single quote
`"  #Double quote

9.5: Creating a basic string

String

Strings are created by wrapping the text with double quotes. Double-quoted strings can evaluate variables and special characters.

1
2
$myString = "Some basic text"
$mySecondString = "String with a $variable"

To use a double quote inside a string it needs to be escaped using the escape character, backtick (`). Single quotes can be used inside a double-quoted string.

1
$myString = "A `" double quoted `" string which also has 'single quotes'."

Literal string

Literal strings are strings that doesn't evaluate variables and special characters. It's created using single quotes.

1
$myLiteralString = 'Simple text including special characters ( `n ) and a $variable-reference'

To use single quotes inside a literal string, use double single quotes or a literal here-string. Double quotes can be used safely inside a literal string

1
$myLiteralString = 'Simple string with ''single quotes'' and "double quotes".'

9.6: Format string

1
$hash = @{ city = 'Berlin' }
1
2
$result = 'You should really visit {0}' -f $hash.city
Write-Host $result #prints "You should really visit Berlin"

Format strings can be used with the -f operator or the static [String]::Format(string format, args) .NET method.